package com.houdoudou.bean;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.http.HttpServletRequest;

import com.houdoudou.common.DateTimeFunction;
public class AppBack {

	HttpServletRequest req=null;
    HttpServletResponse resp=null;
	PrintWriter writer=null;
	JSONObject  jo=null;
	JSONArray ja=null;
	Boolean success=null;
	String errorInfo=null;
	String affair="";	
	public AppBack(HttpServletRequest req,HttpServletResponse resp, Boolean success, String errorInfo) {
		super();
		
		this.req=req; 
		this.resp = resp;
		this.success = success;
		this.errorInfo = errorInfo;
		this.affair=req.getParameter("affair");
		try {	writer= resp.getWriter();	} catch (IOException e) {	e.printStackTrace();}
		
	}

	public AppBack(HttpServletRequest req,HttpServletResponse resp, JSONObject jo, JSONArray ja,
			Boolean success, String errorInfo) {
		super();
		this.req=req; 
		this.resp = resp;
		this.jo = jo;
		this.ja = ja;
		this.success = success;
		this.errorInfo = errorInfo;
		this.affair=req.getParameter("affair");
		try {	writer= resp.getWriter();	} catch (IOException e) {	e.printStackTrace();}
	}


	public AppBack(HttpServletRequest req,HttpServletResponse resp, JSONArray ja, Boolean success,
			String errorInfo) {
		super();
		this.req=req; 
		this.resp = resp;
		this.ja = ja;
		this.success = success;
		this.errorInfo = errorInfo;
		this.affair=req.getParameter("affair");
		try {	writer= resp.getWriter();	} catch (IOException e) {	e.printStackTrace();}
	}

	public AppBack(HttpServletRequest req,HttpServletResponse resp, JSONObject jo, Boolean success,
			String errorInfo) {
		super();
		this.req=req; 
		this.resp = resp;
		this.jo = jo;
		this.success = success;
		this.errorInfo = errorInfo;
		this.affair=req.getParameter("affair");
		try {	writer= resp.getWriter();	} catch (IOException e) {	e.printStackTrace();}
	}

	private void  print(String when,String joName,String jaName ){
		System.out.println( when);
		System.out.println("affair="+affair +"  success="+ success+"  errorInfo="+errorInfo+"  "+joName+"="+jo+"  "+jaName+"="+ja);
		
	}
	
	public  void send(String joName,String jaName){
		print("data back to app------",joName,jaName);
		JSONObject result= new JSONObject();
		result.put("affair",  affair);
		result.put("success", success);
		result.put("errorInfo", errorInfo);
		long currentTime=Long.parseLong(DateTimeFunction.myDateFormat(new Date()));
		result.put("currentTime", currentTime);
		if(!jaName.isEmpty()){ result.put(jaName, ja); }
		if(!joName.isEmpty()){  result.put(joName, jo); }
		writer.print(result.toString());
		writer.close();
		System.out.println(result.toString());
	}
	
	///---------------------------------新的回传函数，一个boolean 无法满足应用了-----------------------------------------//
	
	public AppBack(HttpServletRequest req,HttpServletResponse resp) {
		super();
		this.req=req; 
		this.resp = resp;
		this.affair=req.getParameter("affair");
		try {	writer= resp.getWriter();	} catch (IOException e) {	e.printStackTrace();}
	}
	
	public AppBack(HttpServletRequest req,HttpServletResponse resp, JSONObject jo) {
		super();
		this.req=req; 
		this.resp = resp;
		this.jo = jo;
		this.affair=req.getParameter("affair");
		try {	writer= resp.getWriter();	} catch (IOException e) {	e.printStackTrace();}
	}
	
	public  void send(int resultCode,String resultInfo){	
		JSONObject result= new JSONObject();
		result.put("affair",  affair);
		result.put("resultCode", resultCode);
		result.put("resultInfo", resultInfo);
		long currentTime=Long.parseLong(DateTimeFunction.myDateFormat(new Date()));
		result.put("currentTime", currentTime);   
		
		 if(jo!=null) {result.put("jo", jo);}
	
		System.out.println("---------------data back to app------");
		System.out.println(result.toString());
		writer.print(result.toString());
		writer.close();
	}
}
